
import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCacheDemo1<K, V> extends LinkedHashMap<K, V> {

    private int capacity; //缓存坑位

    public LRUCacheDemo1(int capacity) {
//        super(capacity, 0.75F, false);
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {
        LRUCacheDemo1 lruCacheDemo = new LRUCacheDemo1(3);

        lruCacheDemo.put(1, "a");
        lruCacheDemo.put(2, "b");
        lruCacheDemo.put(3, "c");
        lruCacheDemo.get(1);
        System.out.println(lruCacheDemo.keySet());

        lruCacheDemo.put(4, "d");
        System.out.println(lruCacheDemo.keySet());

        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(3, "c");
        System.out.println(lruCacheDemo.keySet());
        lruCacheDemo.put(5, "x");
        System.out.println(lruCacheDemo.keySet());
    }
}

/**
 *  * true
 *  * [1, 2, 3]
 *  * [2, 3, 4]
 *  * [2, 4, 3]
 *  * [2, 4, 3]
 *  * [2, 4, 3]
 *  * [4, 3, 5]
 *  *
 *  [1, 2, 3]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [3, 4, 5]
 *  
 * <p>
 *  [1, 2, 3]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [3, 4, 5]
 *  
 * <p>
 *  [1, 2, 3]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [3, 4, 5]
 *  
 * <p>
 *  [1, 2, 3]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [3, 4, 5]
 *  
 * <p>
 *  [1, 2, 3]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [3, 4, 5]
 *  
 * <p>
 *  [1, 2, 3]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [3, 4, 5]
 *  
 * <p>
 *  [1, 2, 3]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [3, 4, 5]
 *  
 * <p>
 *  [1, 2, 3]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [2, 3, 4]
 *  [3, 4, 5]
 *  
 */

/**
  [1, 2, 3]
  [2, 3, 4]
  [2, 3, 4]
  [2, 3, 4]
  [2, 3, 4]
  [3, 4, 5]
  */